//
//  RegisterViewController.m
//  图片故事
//
//  Created by 爽 on 16/3/2.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "RegisterViewController.h"
#import "Header.h"
#import "CodeViewController.h"
//#import <SMS_SDK/SMSSDK.h>
#import "CountDownServer.h"
#import <BmobSDK/Bmob.h>
#import "MBProgressHUD+PKX.h"

@interface RegisterViewController (){

    NSString *_oldPhone;
}
@property (nonatomic,strong)UITextField *NumberTextField;
@property (nonatomic,strong)UIButton *StepButton;
@property (nonatomic, assign) BOOL IsFirst;
@property (nonatomic,strong) UILabel    *Label;

@end

@implementation RegisterViewController

#pragma mark -- 懒加载

-(UILabel *)Label {
    if (!_Label) {
        _Label = [[UILabel alloc] initWithFrame:CGRectMake(My_Width(5), My_Height(100), SCREEN_W, My_Height(40))];
        _Label.text = @"请输入您本人的电话号码，以方便注册。";
        _Label.textColor = [UIColor grayColor];
        _Label.font = [UIFont boldSystemFontOfSize:16.0];
        _Label.numberOfLines = 0;
        
    }
    return _Label;
}

//手机验证码
-(UITextField *)NumberTextField {
    if (!_NumberTextField) {
        UIView *View = [[UIView alloc]initWithFrame:CGRectMake(My_Width(0), My_Height(0), My_Width(20), My_Height(52))];
        _NumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(My_Width(0), My_Height(160), SCREEN_W , My_Height(50))];
        _NumberTextField.placeholder = @"请输入手机号码";
        [_NumberTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
        _NumberTextField.font = [UIFont systemFontOfSize:16.0];
        [_NumberTextField setTextColor:[UIColor grayColor]];
        _NumberTextField.backgroundColor = Btn_Color;
        _NumberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _NumberTextField.leftView = View;
        _NumberTextField.leftViewMode = UITextFieldViewModeAlways;//光标位置
    }
    return _NumberTextField;
}

//下一步
-(UIButton *)StepButton {
    if (!_StepButton) {
        _StepButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _StepButton.frame = CGRectMake(My_Width(20), My_Height(250), SCREEN_W - My_Width(40), My_Height(50));
        [_StepButton setTitle:@"下一步" forState:UIControlStateNormal];
        _StepButton.backgroundColor = [UIColor colorWithRed:102 / 255.0 green:214 / 255.0 blue:228 / 255.0 alpha:1];
        [_StepButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _StepButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        _StepButton.layer.cornerRadius = 5;
        [_StepButton addTarget:self action:@selector(Register) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _StepButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];

     self.title = @"验证手机号码";
     self.view.backgroundColor = Bg_Color;
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(returnbutton)];
    
    if ([_identifier isEqualToString:@"forget"]) {
        self.Label.text = @"请输入您本人的电话号码，以方便找回密码。";
    }
    
     self.navigationItem.leftBarButtonItem = backItem;
    
    [self.view addSubview:self.NumberTextField];
    [self.view addSubview:self.StepButton];
    [self.view addSubview:self.Label];
}

#pragma mark -- 点击事件

-(void)returnbutton {
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)Register {
    NSString *mobile = self.NumberTextField.text;
    if (mobile.length == 0) {
        
        [MBProgressHUD showError:@"手机号码不能为空"];
    } else if (mobile.length != 11) {
        
        [MBProgressHUD showError:@"手机号只有11位"];
    } else {
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        
        if (isMatch1 || isMatch2 || isMatch3) {
            
            if (![mobile isEqualToString:_oldPhone]) {
                
                BmobQuery *query = [[BmobQuery alloc] init];
                
                NSString *sql = [NSString stringWithFormat:@"select count(*) from user where userName = '%@'",mobile];
                
                [query queryInBackgroundWithBQL:sql block:^(BQLQueryResult *result, NSError *error) {
                    if (error) {
                        
                        NSLog(@"%@",[error localizedDescription]);
                    } else {
                        
                        if ([_identifier isEqualToString:@"regist"]) {
                        
                            if (result.count>0) {
                            
                                [MBProgressHUD showError:@"手机号已经注册"];
                            
                            } else {
                                
                                [self TestGetCodeWithMobile:mobile];
                                
                            }
                        } else if ([_identifier isEqualToString:@"forget"]) {
                            
                            if (result.count>0) {
                                
                                [self TestGetCodeWithMobile:mobile];
                                
                            } else {
                                
                                [MBProgressHUD showError:@"手机号没有注册"];
                            }
                        }
                    }
                }];
                
            } else {
               
            }
            
        }else{
            [MBProgressHUD showError:@"请输入正确的手机号码"];
        }
    }
}

- (void)TestGetCodeWithMobile:(NSString *)mobile {
    
    if (![CountDownServer isCountDowning:@"CountDownForVerifyCode"]) {

        /*
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:mobile zone:@"86" customIdentifier:nil result:^(NSError *error) {
            if (error) {
                
                [MBProgressHUD showError:@"获取验证码失败"];
            }else {
                _oldPhone = mobile;
                CodeViewController * codeVc = [[CodeViewController alloc] init];
                codeVc.phoneNumStr = self.NumberTextField.text;
                codeVc.identifier = _identifier;
                [self.navigationController pushViewController:codeVc animated:YES];
            }
        }];
         */
    }
}


@end
